home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Chess++ ƒ / CChessPieces ƒ / CKnight.cp < prev    next >
Text File  |  1993-05-26  |  3KB  |  127 lines

  1. ////////////
  2. //
  3. //    CKnight.cp
  4. //
  5. //    Chess Piece methods for implementing a Knight.
  6. //
  7. //    Copyright © 1993 Steven J. Bushell. All rights reserved.
  8. //
  9. ////////////
  10.  
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #include "CChessBoard.h"
  15. #include "CChessPiece.h"
  16. #include "CKnight.h"
  17.  
  18. extern    CIconHandle    gWhiteKnightCicnHandle;
  19. extern    CIconHandle    gBlackKnightCicnHandle;
  20.  
  21. void CKnight::IKnight(Boolean aColor)
  22. {
  23.     inherited::IChessPiece(aColor);
  24.     itsValue = 0x0300;
  25. }
  26.  
  27. void CKnight::Draw(short rank, short file)
  28. {
  29.     CIconHandle cicnHandle;
  30.     Rect aRect;
  31.     short rankQD = (rank - 1) << 5, fileQD = (file - 1) << 5;
  32.     
  33.     if (itsColor == White)
  34.         cicnHandle = gWhiteKnightCicnHandle;
  35.     else
  36.         cicnHandle = gBlackKnightCicnHandle;
  37.  
  38.     aRect.left = rankQD;
  39.     aRect.right = rankQD+32;
  40.     aRect.top = fileQD;
  41.     aRect.bottom = fileQD+32;
  42.     PlotCIcon(&aRect,cicnHandle);
  43. }
  44.  
  45. CIconHandle    CKnight::GetCicnHandle(void)
  46. {
  47.     return gWhiteKnightCicnHandle;
  48. }
  49.  
  50. Boolean    CKnight::IsValidMove(CChessBoard *aBoard, short newRank, short newFile)
  51. {
  52.     short    oldRank = aBoard->firstClickRank, oldFile = aBoard->firstClickFile;
  53.     
  54.     if ((abs(newRank-oldRank) == 2) && (abs(newFile-oldFile) == 1))
  55.         return true;
  56.     if ((abs(newRank-oldRank) == 1) && (abs(newFile-oldFile) == 2))
  57.         return true;
  58.     return false;
  59. }
  60.  
  61.  
  62. static CChessPiece *GetPiece(CChessBoard *board,short rank,short file)
  63. {
  64.     if (rank<1)
  65.         return NULL;
  66.     if (rank>8)
  67.         return NULL;
  68.     if (file<1)
  69.         return NULL;
  70.     if (file>8)
  71.         return NULL;
  72.         
  73.     return board->theBoard[rank][file];
  74. }
  75.  
  76.  
  77. short    CKnight::BoardLocationValue(CChessBoard *aBoard, short rank, short file)
  78. {
  79.     register short    thisPieceColor = itsColor;
  80.     CChessPiece        *target;
  81.     short            totalValue;
  82.     long            pieceValues = 0;
  83.  
  84.     totalValue = inherited::BoardLocationValue(aBoard,rank,file);
  85.  
  86.     target = GetPiece(aBoard,rank-1,file-2);
  87.     if (target)
  88.         if (thisPieceColor == target->itsColor)
  89.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  90.  
  91.     target = GetPiece(aBoard,rank-1,file+2);
  92.     if (target)
  93.         if (thisPieceColor == target->itsColor)
  94.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  95.  
  96.     target = GetPiece(aBoard,rank+1,file-2);
  97.     if (target)
  98.         if (thisPieceColor == target->itsColor)
  99.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  100.  
  101.     target = GetPiece(aBoard,rank+1,file+2);
  102.     if (target)
  103.         if (thisPieceColor == target->itsColor)
  104.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  105.  
  106.     target = GetPiece(aBoard,rank-2,file-1);
  107.     if (target)
  108.         if (thisPieceColor == target->itsColor)
  109.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  110.  
  111.     target = GetPiece(aBoard,rank-2,file+1);
  112.     if (target)
  113.         if (thisPieceColor == target->itsColor)
  114.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  115.  
  116.     target = GetPiece(aBoard,rank+2,file-1);
  117.     if (target)
  118.         if (thisPieceColor == target->itsColor)
  119.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  120.  
  121.     target = GetPiece(aBoard,rank+2,file+1);
  122.     if (target)
  123.         if (thisPieceColor == target->itsColor)
  124.             pieceValues += (target->itsValue == kKingValue) ? 0 : target->itsValue;
  125.  
  126.     return totalValue + (pieceValues >> 10);
  127. }